import { createRouter, createWebHashHistory } from 'vue-router'
// import Home from '../home/home.vue';
import Home from '../page/home/home.vue';
import VideoTranscoding from '../page/videoTranscoding/videoTranscoding.vue'
import videoUpload from '../page/videoUpload/videoUpload.vue';
import course from '../page/courseList/courseList.vue';
import Login from '../login/login.vue'
import useLoginStore from '../store/index'
import { createPinia, storeToRefs } from 'pinia';
import App from '../App.vue'
import { createApp } from 'vue';
const routes = [
    {
        path: '/',
        component: Home,
        children: [
            { path: 'videoUpload', component: videoUpload }, 
            { path: 'course', component: course },    
            { path: 'videoTranscoding',name:"videoTranscoding", component: VideoTranscoding }, 
        ],
        meta: {
            title: '首页',
        }
    },
    { path: '/login', component: Login },
]

const router = createRouter({
    history: createWebHashHistory(import.meta.env.VITE_BASE_PATH as string),
    routes, // `routes: routes` 的缩写
})
//路由拦截
router.beforeEach((to, from, next) => {
    const pinia = createPinia()
    const app = createApp(App)
    app.use(router)
    app.use(pinia)
    const store: any = useLoginStore()
    const { userInfo } = storeToRefs(store)
    console.log(userInfo.value, '路由用户信息')
    if (to.path === '/login') {
        if (userInfo.value.id) {
            next('/')
        } else {
            next()
        }
    } else {
        console.log(1)
        if (userInfo.value.id) {
            console.log(2)
            next()
        } else {
            console.log(3)
            next('/login')
        }
    }
})
export default router